﻿/*
* the initail mod tab
* @Author:      dingyantao
* @CreateDate:  2013-10-12
*/
(function ($) {
    var TabControl = function (pObj, pOpts) {
        pOpts = $.extend({
            btns: null,
            initView: null,
            tabClass: ["", "on"],
            index: 0,
            url: null,
            tempalte: null,            
            views: null,
            eventType: "click",
            querySelecterKey:"key",            
            ajaxId:"locationId",
            ajaxFun:null,            
            onLoad: null,
            onFisrtLoad: null
        }, pOpts);
        this.target = $(pObj);
        this.init(pOpts);
    };
    TabControl.prototype = {
        init: function (pOpts) {
            this.lastKey = null;
            this.currentKey = null;
            //tab页缓存
            this.cache = {};
            //btn指针
            this.btnPoint = {},
            this.setOptions(pOpts);
            this.bindEvent();
        },
        setOptions: function (pOpts) {
            $.extend(this, pOpts);
            if (this.initView !== null) {
                var _btn = $(this.btns[this.index]),
                    _key = _btn.data(this.querySelecterKey);
                this.cache[_key] = this.initView;
                this.currentKey = _key;
                this.btnPoint[_key] = _btn;
                _btn.addClass(this.tabClass[1]);
            }
        },
        Switch: function (pKey) {
            if (this.currentKey !== null) {
                this.hide();
                this.show(pKey);
            }
        },
        hide: function () {
            this.btnPoint[this.currentKey].removeClass(this.tabClass[1]);
            this.cache[this.currentKey].hide();
        },
        show: function (pKey) {
            this.currentKey = pKey;
            this.btnPoint[pKey].addClass(this.tabClass[1]);
            this.cache[pKey].show();
            if ($.type(this.onLoad) === "function") this.onLoad(this.cache[pKey], this.btnPoint[pKey]);
        },
        bindEvent: function () {
            var me = this;
            if (this.url && this.initView) {
                this.btns.bind(me.eventType, function () {
                    if ($(this).hasClass(me.tabClass[1])) return;
                    var _this = $(this),
                    _key = _this.data(me.querySelecterKey);
                    if (me.cache[_key] == null) {
                        var _div = $("<div></div>");
                        me.target.append(_div);
                        me.cache[_key] = _div;
                        me.btnPoint[_key] = _this;
                        if(me.ajaxFun){
                            var _obj = {};
                            _obj[me.ajaxId] = _key;
                            $.post(me.url, _obj, function (data) {
                                _div.html($.template.replace(me.tempalte, { data: $.parseJSON(data) }));
                                me.Switch(_key);
                                if ($.type(me.onFisrtLoad) === "function") me.onFisrtLoad(_div, _this);
                            });
                        }else{
                            if($.type(me.ajaxFun) === "function"){
                                me.ajaxFun.apply(me,_key,_div,_this);
                            }
                        }
                    } else {
                        me.Switch(_key);
                    }
                });
            } else {
                for (var i = this.btns.length - 1; i >= 0; i--) {
                    this.btnPoint[i] = $(this.btns[i]);
                    this.cache[i] = $(this.views[i]);
                    (function (index) {
                        me.btnPoint[index].bind(me.eventType, function () {
                            if me.btnPoint[index].hasClass(me.tabClass[1])) return;
                            me.Switch(index);
                        });
                    })(i);
                };
                this.currentKey = this.index;
                this.Switch(this.index);
            }
        }
    };
    $.fn.extend({
        Tab: function (pOpts) {
            var _defaults = {};
            if (typeof pOpts !== "undefined") {
                $.extend(true, _defaults, pOpts);
            }
            TabControl["instance"] = new TabControl(this, _defaults);
            return TabControl["instance"];
        }
    });
})(jQuery);